package com.mrzhiyi.demo.sb.common.resp;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 响应结果类
 *
 * @param <T>
 *     任意类型
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Result<T>{
    private Integer code;
    private String message;
    private T data;

    public static <T> Result<T> ok(){
        return ok(RespCode.SUCCESS,null,null);
    }

    public static <T> Result<T> ok(T data){
        return ok(RespCode.SUCCESS,RespCode.SUCCESS.msg(),data);
    }

    public static <T> Result<T> ok(String message,T data){
        return ok(RespCode.SUCCESS,message,data);
    }

    public static <T> Result<T> error(){
        return error(RespCode.ERROR,RespCode.ERROR.msg(),null);
    }

    public static <T> Result<T> error(String message){
        return error(RespCode.ERROR,message,null);
    }

    public static <T> Result<T> error(RespCode code,String message){
        return error(code,message,null);
    }

    public static <T> Result<T> ok(RespCode code,String message,T data){
        Result<T> r=new Result<>();
        r.setCode(code.code());
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> error(RespCode code,String message,T data){
        Result<T> r=new Result<>();
        r.setCode(code.code());
        r.setMessage(message);
        r.setData(data);
        return r;
    }
}
